package com.longnuoqi.mydoctor.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;


import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.longnuoqi.mydoctor.activity.DoctorAskActivity;
import com.longnuoqi.mydoctor.adapter.DoctorListViewAdapter;
import com.longnuoqi.mydoctor.entity.Ask;
import com.longnuoqi.mydoctor.utils.FileUtil;
import com.yunhe.mydoctor.R;

import java.util.List;


/**
 * Created by Lenovo on 2015/12/26.
 */
public class DoctorListFragment extends Fragment {
    private ListView listView;
    private DoctorListViewAdapter adapter;
    private int id;
    private String url;
    private String name;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.doctor_list_layout, container, false);
        savedInstanceState = getArguments();
        id = savedInstanceState.getInt("id");
        url = "http://www.tngou.net/api/ask/list?id=" + id + "&rows=8";
        name = "ask_list_" + id + "_part.txt";
        getData(view, url);
        return view;
    }

    private void getData(View view, String url) {
        listView = (ListView) view.findViewById(R.id.doctor_listview);
        if (!FileUtil.fileIsExist(name)) {
            Log.e("===================", "doctor file:" + name + "is not exist");
            RequestQueue queue = Volley.newRequestQueue(getActivity());
            StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    FileUtil.saveFile(s, name);
                    initListView(s);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                }
            });
            queue.add(request);
        } else {
            Log.e("===================", "doctor file:" + name + " exists");
            String s = FileUtil.readFile(name);
            initListView(s);
        }
    }

    private void initListView(String s) {
        List<Ask> list = FileUtil.jsonAskList(s);
        adapter = new DoctorListViewAdapter(getActivity(), list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int askId = (int) (view.findViewById(R.id.doctor_item_content_txt)).getTag();
                Intent intent = new Intent(getActivity(), DoctorAskActivity.class);
                intent.putExtra("id", askId);
                startActivityForResult(intent, 0);
            }
        });
    }


}
